<?php
require_once('table.php');
if( !isset($config) ){
	$config = array();
}
require_once(APPPATH . 'config/artist_user.php');

class ArtistImages extends Table{
	var $menu_view = 'includes/artist_menu';
	
	function ArtistImages(){
		global $config;
		parent::Table($config['artist_user_config']);
		$this->load->model('artist_images_model');
		$this->model = $this->artist_images_model;
	}
	
	function _getUserImageIds($where = null){
		$user_image_ids = $this->db->query("SELECT image_id FROM artist_images WHERE id = " . $this->session->userdata('userid') . ($where?" AND $where":''));
		$user_image_ids = $user_image_ids->result_array();
		$user_image_ids = array_map(create_function('$e', 'return $e[\'image_id\'];'), $user_image_ids);
		return $user_image_ids;
		//return array_filter($user_image_ids->result_array(), create_function('$e', 'return $e[\'image_id\'];'));
	}
	
	function _checkId($id){
		$user_image_ids = $this->_getUserImageIds();
		$uploaded_image_ids = $this->session->userdata('uploaded_images');
		if( $uploaded_image_ids ){
			$uploaded_image_ids = unserialize($uploaded_image_ids);
		}
		if(!is_array($uploaded_image_ids)){
			$uploaded_image_ids = array();
		}
		if( !in_array($id, $user_image_ids) && !in_array($id, $uploaded_image_ids)){
			$this->session->set_flashdata('cmf_message', 'You do not have permission to access this item');
			redirect(CMF_URL . $this->controller . '/error');
			return false;
		}
	}
	
	function _getValidImageIds($ids){
		$where = 'IN (' . implode(',', $ids) . ')';
		return $this->_getUserImageIds($where);
	}
	
	/* model will only show images with correct artist id, the following functions must still
	 * check the ids to avoid operating on other artist's images */
	function delete($id, $ajax = 0){
		$this->_checkId($id);
		return parent::delete($id);
	}
	
	function edit( $id = null, $view = CMF_DEFAULT_TABLE_EDIT_VIEW, $data = array() ){
		$this->_checkId($id);
		return parent::edit($id, $view, $data);
	}
	
	function editAjax( $id = null, $view = CMF_DEFAULT_TABLE_AJAX_EDIT_VIEW, $data = array()){
		$this->_checkId($id);
		return parent::editAjax($id, $view, $data);
	}
	
	function editMultiple( $return = null ){
		if( $this->input->post('selected') && is_array($this->input->post('selected'))){
			$_POST['selected'] = $this->_getValidImageIds($this->input->post('selected'));	
		}
		return parent::editMultiple( $return );
	}
	
	function setLive($id = null){
		$this->_checkId($id);
		return parent::setLive($id);
	}
	
	function setNotLive($id = null){
		$this->_checkId($id);
		return parent::setNotLive($id);
	}
	
	function archive($id = null){
		$this->_checkId($id);
		return parent::archive($id);
	}
	
	function restore($id = null){
		$this->_checkId($id);
		return parent::restore($id);
	}
	
	function ajaxItemAccess($id){
		$this->_checkId($id);
		return parent::ajaxItemAccess($id);
	}
	
	function save($id = null, $ajax = 0){
		$this->_checkId($id);
		parent::save($id, $ajax);
	}
	
	function detail($id){
		$this->_checkId($id);
		$item = $this->model->getItem($id, true);
		$this->load->view('cmf/image_detail', array('item' => $item));
	}
}
?>